<template>
	<section class="sp-section sp-disinfect" style="padding-bottom: 100rpx;">
		<!-- 第一步提示 -->
		<template v-if="step == 1">
			<view class="sp-disinfect__error">
				<image src="../../static/images/disinfect/dry1.png" style="width: 432rpx;" mode="widthFix" />
			</view>
			<view class="sp-disinfect__tips">
				物资请勿叠放，未拆封物资直接放，拆封后的物资请完全打开后放入，请设置55℃，确保消毒到位，物资全部放入后拍照上传
			</view>
			<!-- 创建任务 @click="createTask" -->
			<view class="sp-disinfect__next" @click="chooseImage">拍摄</view>
		</template>
		<!-- 第二步提示 -->
		<template v-if="step == 2">
			<view class="sp-disinfect__error">
				<image src="../../static/images/disinfect/dry2.svg" mode="widthFix" />
			</view>
			<view class="sp-disinfect__tips" style="text-align: center;">
				请将工作区域完全消毒后离开
			</view>
			<view class="sp-disinfect__next" @click="backTo">消毒完成，已离开</view>
		</template>
		<!-- 第三步完成提示 -->
		<template v-if="step == 3">
			<view class="sp-disinfect__error">
				<image src="../../static/images/disinfect/dry2.png" style="width: 326rpx;" mode="widthFix" />
			</view>
			<view class="sp-disinfect__tips">
				烘干物资55摄氏度以上保持4个小时，手机登录联网温度记录仪系统，请截图上传
			</view>
			<!-- 创建任务 @click="createTask" -->
			<view class="sp-disinfect__next" @click="chooseImageAsAlbum">上传图片</view>
		</template>
		<!-- 第四步完成提示 -->
		<template v-if="step == 4">
			<view class="sp-disinfect__error">
				<image src="../../static/images/disinfect/dry3.png" mode="widthFix" />
			</view>
			<view class="sp-disinfect__tips" style="text-align: center;">
				消毒烘干已完成，请取走
			</view>
			<view class="sp-disinfect__next" @click="updateTask">已取走全部消毒物资</view>
		</template>
	</section>
</template>

<script>
	import { setTask, uploadImage, uptTask } from '@/api/task.js'
	import dayjs from '../../utils/dayjs.min.js'
	
	export default {
		name: 'spDisinfectDry',
		data() {
			return {
				step: 1,
				form: {
					type: '烘干',
					stage: null
				},
				taskId: 30,
				tempFile: [],
				tempFilePaths: []
			}
		},
		onLoad(options) {
			console.log('options', options)
			this.form = {
				...this.form,
				building_id: options.id,
				building_name: options.name,
				title: `${options.name}-烘干`,
				description: `${options.name}-烘干-${new Date().getTime()}`,
			}
			if (options.mid) {
				this.taskId = options.mid
				this.step = options.stage == 1 ? 4 : 3
				// if (options.stage == 1) {
				// 	this.step = 3
				// }
			}
			// if (options.stage == 0 && options.mid) {
			// 	this.step = 2
			// }
		},
		mounted() {},
		methods: {
			backTo() {
				uni.setStorage({
					key: 'refresh',
					data: true,
					success: () => {
						uni.navigateBack()
					}
				})
			},
			// 新建任务
			createTask() {
				const infos = uni.getStorageSync('aiUserInfo')
				const params = JSON.parse(JSON.stringify(this.form))
				params.stage = this.step - 1
				setTask(infos.id, params).then(res => {
					if (res && res.id) {
						// this.$u.toast('创建成功')
						this.taskId = res.id
						// this.chooseImage(res.id)
						this.setUploadPics(res.id, 2)
					} else {
						this.$u.toast('任务创建失败')
					}
				}).catch(err => {
					console.log('创建任务失败了', err)
				})
			},
			// 选择图片
			chooseImage () {
				// 直接打开下一个弹窗
				uni.chooseImage({
					count: 1,
					sizeType: ['compressed'],
					sourceType: ['camera', 'album'],
					success: (temp) => {
						this.tempFile = temp.tempFiles
						this.tempFilePaths = temp.tempFilePaths
						this.createTask();
					},
					fail: (err) => {
						console.log('err', err)
					},
					complete: () => {
						console.log('调用结束')
					}
				})
			},
			// 选择截图
			chooseImageAsAlbum () {
				// 直接打开下一个弹窗
				uni.chooseImage({
					count: 1,
					sizeType: ['compressed'],
					sourceType: ['album'],
					success: (temp) => {
						this.tempFile = temp.tempFiles
						this.tempFilePaths = temp.tempFilePaths
						this.setUploadPics(this.taskId, 4);
					},
					fail: (err) => {
						console.log('err', err)
					},
					complete: () => {
						console.log('调用结束')
					}
				})
			},
			// 上传图片
			setUploadPics (id, step) {
				if (this.tempFile.length === 0) {
					this.$u.toast('请先选择图片')
					return false
				}
				const infos = uni.getStorageSync('aiUserInfo')
				const token = uni.getStorageSync('token')
				uni.showToast({
					title: '正在上传...',
					icon: 'loading',
					mask: true,
					duration: 10000
				})
				let uploadImgCount = 0
				for (let i = 0; i < this.tempFile.length; i++) {
					// console.log('ces 装备上传', this.tempFilePaths[i])
					uni.uploadFile({
						url: `${uploadImage}/${infos.id}/${id}`,
						filePath: this.tempFilePaths[i],
						name: 'file',
						header: {
							Authorization: `bearer ${token}`
						},
						success: (res) => {
							uploadImgCount++
							const data = JSON.parse(res.data)
							if (data) {
								uni.hideToast()
								this.step = step
								this.$u.toast('创建成功');
							}
						},
						fail: (err) => {
							uni.showModal({
								title: '错误提示',
								content: '上传图片失败',
								showCancel: false
							})
						}
					})
				}
			},
			// 更新任务状态
			updateTask() {
				const params = {
					stage: 4,
					is_return: 0
				}
				// const infos = uni.getStorageSync('aiUserInfo')
				uptTask(this.taskId, params).then(res => {
					this.$u.toast('取出成功')
					setTimeout(() => {
						this.backTo()
					}, 1500)
				}).catch(err => {
					this.$u.toast('取出失败')
				})
			}
		}
	}
</script>

<style>
</style>